setUpBeforeClass{
  StaticHandler.addDelegate(new JPAHandler());
  StaticHandler.addDelegate(new RepoHandler());
  StaticHandler.addDelegate(new RuntimeHandler(){
    @Override public void beforeBeanDiscovery(    BeforeBeanDiscovery bbd,    BeanManager bm){
      bbd.addAnnotatedType(bm.createAnnotatedType(ClusterComponent.class));
      bbd.addAnnotatedType(bm.createAnnotatedType(ClusterAssistant.class));
      super.beforeBeanDiscovery(bbd,bm);
    @Override public void afterDeploymentValidation(    AfterDeploymentValidation adv,    BeanManager bm){
      this.manage(ClusterComponent.class);
      this.manage(ClusterAssistant.class);
      Set<Bean<?>> beans=bm.getBeans(Repository.class,new AnnotationLiteral<Any>(){
      if (beans.size() > 0) {
        Bean<Repository> repoBean=(Bean<Repository>)beans.iterator().next();
        CreationalContext<Repository> ctx=bm.createCreationalContext(repoBean);
        Repository repo=(Repository)bm.getReference(repoBean,Repository.class,ctx);
          Unmarshaller u=Cluster.CLUSTER_JAXB_CTX.createUnmarshaller();
          JAXBElement<ClusterConfig> config=(JAXBElement<ClusterConfig>)u.unmarshal(new ByteArrayInputStream(content));
          clusterConfig=config.getValue();
          repo.create(new QName(Cluster.CLUSTER_CONFIG_NAMESPACE,clusterId),Cluster.CLUSTER_CONFIG_MIMETYPE,"1.0",content);
          log.log(Level.SEVERE,"",e);
        repoBean.destroy(repo,ctx);
      super.afterDeploymentValidation(adv,bm);
      cluster=(Cluster)getInstance(Cluster.class);
      assistant=(ClusterAssistant)getInstance(ClusterAssistant.class);
      assistant.setup(testNodeId,clusterId,clusterConfig);
      clusterComponent=(ClusterComponent)getInstance(ClusterComponent.class);
    @Override public void beforeShutdown(    BeforeShutdown adv,    BeanManager bm){
  server=new Server();
}
beforeBeanDiscovery{
  bbd.addAnnotatedType(bm.createAnnotatedType(ClusterComponent.class));
  bbd.addAnnotatedType(bm.createAnnotatedType(ClusterAssistant.class));
}
afterDeploymentValidation{
  this.manage(ClusterComponent.class);
  this.manage(ClusterAssistant.class);
  Set<Bean<?>> beans=bm.getBeans(Repository.class,new AnnotationLiteral<Any>(){
  if (beans.size() > 0) {
    Bean<Repository> repoBean=(Bean<Repository>)beans.iterator().next();
    CreationalContext<Repository> ctx=bm.createCreationalContext(repoBean);
    Repository repo=(Repository)bm.getReference(repoBean,Repository.class,ctx);
      JAXBElement<ClusterConfig> config=(JAXBElement<ClusterConfig>)u.unmarshal(new ByteArrayInputStream(content));
      clusterConfig=config.getValue();
      repo.create(new QName(Cluster.CLUSTER_CONFIG_NAMESPACE,clusterId),Cluster.CLUSTER_CONFIG_MIMETYPE,"1.0",content);
      log.log(Level.SEVERE,"",e);
    repoBean.destroy(repo,ctx);
  super.afterDeploymentValidation(adv,bm);
  cluster=(Cluster)getInstance(Cluster.class);
  assistant=(ClusterAssistant)getInstance(ClusterAssistant.class);
  assistant.setup(testNodeId,clusterId,clusterConfig);
  clusterComponent=(ClusterComponent)getInstance(ClusterComponent.class);
}
beforeShutdown{ }
tearDownAfterClass{
  server.stop();
}
testNodeAutoDiscovery{
  cluster.offline();
  assistant.offline();
  nodeStatus(assistant.availableNodes(),false,false);
  assistant.online();
  nodeStatus(assistant.availableNodes(),false,true);
  cluster.online();
  assistant.healthCheck();
  nodeStatus(assistant.availableNodes(),true,true);
  cluster.offline();
  assistant.offline();
  nodeStatus(assistant.availableNodes(),false,false);
}
nodeStatus{
  assertEquals(2,status.size());
  for (  NodeStatus s : status) {
    if (nodeId.equals(s.nodeId())) {
      if (node1) {
        assertTrue(NodeState.ONLINE.equals(s.state()));
 else {
        assertTrue(NodeState.OFFLINE.equals(s.state()));
 else     if (testNodeId.equals(s.nodeId())) {
      if (node2) {
        assertTrue(NodeState.ONLINE.equals(s.state()));
 else {
        assertTrue(NodeState.OFFLINE.equals(s.state()));
 else {
}
testHeartbeat{
  Calendar first=assistant.getHeartBeat(nodeId);
    Calendar second=assistant.getHeartBeat(nodeId);
    if (second.compareTo(first) > 0) {
      passed=true;
  assertTrue(passed);
}
testLocalAction{
  assertNotNull(cluster);
  assertNotNull(clusterComponent);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_LOCAL_ACTION,TaskType.ACTION,LocalTestAction.getProvider()));
  ActionId id=cluster.execute(ClusterComponent.TEST_LOCAL_ACTION,ClusterComponent.testActionDoc("localTest"),Target.LOCAL);
  assertNotNull(id);
  LocalTestAction.notify(2,TimeUnit.SECONDS);
  ActionStatus status=cluster.status(id);
  assertNotNull(status);
  assertEquals(ActionState.START,status.state());
  LocalTestAction.notify(2,TimeUnit.SECONDS);
  LocalTestAction.notify(2,TimeUnit.SECONDS);
  assertNotNull(status);
  assertEquals(ActionState.EXECUTING,status.state());
  LocalTestAction.notify(2,TimeUnit.SECONDS);
  LocalTestAction.notify(2,TimeUnit.SECONDS);
  assertNotNull(status);
  assertEquals(ActionState.FINISH,status.state());
  LocalTestAction.notify(2,TimeUnit.SECONDS);
    if (ActionState.COMPLETED.equals(status.state())) {
      assertNotNull(status.start());
      assertNotNull(status.finish());
  assertTrue(passed);
}
testRemoteAction{
  assertNotNull(cluster);
  assertNotNull(clusterComponent);
  assertNotNull(assistant);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_REMOTE_ACTION,TaskType.ACTION,RemoteTestAction.getProvider()));
  ActionId id=assistant.executeRemoteAction(ClusterComponent.testActionDoc("remoteTest"),nodeId);
  assertNotNull(id);
    ActionStatus status=cluster.status(id);
    assertNotNull(status);
    if (ActionState.COMPLETED.equals(status.state())) {
      assertNotNull(status.start());
      assertNotNull(status.finish());
  assertTrue(passed);
  assertNotNull(result);
}
testMasterSlaveAction{
  assertNotNull(cluster);
  assertNotNull(clusterComponent);
  assertNotNull(assistant);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_MASTER_SLAVE_ACTION,TaskType.MASTER,MasterTestAction.getProvider()));
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_MASTER_SLAVE_ACTION,TaskType.SLAVE,SlaveTestAction.getProvider()));
  for (int i=0; i < 50; i++) {
    ActionStatus status=cluster.status(id);
    assertNotNull(status);
      assertNotNull(status.start());
      assertNotNull(status.finish());
      passed=true;
  assertTrue(passed);
  assertNotNull(result);
}
testUpdateStatus{
  assertNotNull(clusterComponent);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_STATUS_ACTION,TaskType.ACTION,StatusTestAction.getProvider()));
  ActionId id=cluster.execute(ClusterComponent.TEST_STATUS_ACTION,null,Target.LOCAL);
  assertNotNull(id);
  for (int i=0; i < 50; i++) {
    ActionStatus status=cluster.status(id);
    assertNotNull(status);
    if (ActionState.FAILED.equals(status.state())) {
    Thread.sleep(100);
  assertTrue(passed);
}
testLogMessages{
  assertNotNull(cluster);
  assertNotNull(clusterComponent);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_LOG_ACTION,TaskType.ACTION,LogTestAction.getProvider()));
  ActionId id=cluster.execute(ClusterComponent.TEST_LOG_ACTION,null,Target.LOCAL);
  List<ActionMessage> messages=null;
    ActionStatus status=cluster.status(id);
    if (ActionState.FAILED.equals(status.state())) {
      messages=status.messages();
      assertNotNull(status.start());
      assertNotNull(status.finish());
      passed=true;
  assertTrue(passed);
  assertEquals(LogLevel.INFO,messages.get(0).getType());
  assertEquals("start",messages.get(0).getMessage());
  assertEquals(LogLevel.WARNING,messages.get(1).getType());
  assertEquals("run",messages.get(1).getMessage());
  assertEquals(LogLevel.ERROR,messages.get(2).getType());
  assertEquals("finish",messages.get(2).getMessage());
  assertEquals(LogLevel.ERROR,messages.get(3).getType());
  assertEquals("exception",messages.get(3).getMessage());
}
testCancel{
  assertNotNull(cluster);
  assertNotNull(clusterComponent);
  clusterComponent.actions().clear();
  clusterComponent.actions().add(new TaskActionImpl(ClusterComponent.TEST_CANCEL_ACTION,TaskType.ACTION,CancelTestAction.getProvider()));
  ActionId id=cluster.execute(ClusterComponent.TEST_CANCEL_ACTION,ClusterComponent.testActionDoc("start"),Target.LOCAL);
  for (int i=0; i < 50; i++) {
    ActionStatus status=cluster.status(id);
    if (status.start() != null && ActionState.START.equals(status.state())) {
 else     if (status.finish() != null && ActionState.CANCELED.equals(status.state())) {
      assertNotNull(status.start());
      assertNotNull(status.finish());
      break;
    Thread.sleep(100);
  assertTrue(passed);
  id=cluster.execute(ClusterComponent.TEST_CANCEL_ACTION,ClusterComponent.testActionDoc("run"),Target.LOCAL);
  for (int i=0; i < 50; i++) {
    ActionStatus status=cluster.status(id);
    if (ActionState.EXECUTING.equals(status.state())) {
 else     if (status.finish() != null && ActionState.CANCELED.equals(status.state())) {
      assertNotNull(status.start());
      assertNotNull(status.finish());
      assertEquals("finish",result.getDocumentElement().getTextContent());
      break;
    Thread.sleep(100);
  assertTrue(passed);
}
